const jwt = require('jsonwebtoken');

const SECRET_KEY = 'wnsdbysj';  
const DEFAULT_EXPIRES = '1d';          // 默认1天
const REMEMBER_EXPIRES = '7d';         // 记住我7天

const generateToken = (payload, remember = false) => {
  return jwt.sign(payload, SECRET_KEY, {
    expiresIn: remember ? REMEMBER_EXPIRES : DEFAULT_EXPIRES
  });
};

const verifyToken = (token) => {
  try {
    return jwt.verify(token, SECRET_KEY);
  } catch (error) {
    // 专门处理 token 过期的情况
    if (error.name === 'TokenExpiredError') {
      error.message = '登录已过期，请重新登录';
    }
    throw error;  // 抛出错误，让上层处理
  }
};

module.exports = {
  generateToken,
  verifyToken,
  SECRET_KEY  
}; 